﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Rotempco.Core.Web.Objects;
using Rotempco.Core.Web.Objects.Enums;

namespace Rotempco.Core.Web.UserControls
{
    public class ControlX : WebControl
    {
        Control Control;

        public ControlX()
        {
            EnsureChildControls();
            if (controlXState == null)
                controlXState = new ControlXState();
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterRequiresControlState(this);
        }

        public string FieldOptionsPropertyName { get; set; }
        public string FieldValuePropertyName { get; set; }

        public string FieldValue
        {
            get
            {
                if (FieldType == FieldType.CheckBox )
                {
                    if (controlXState.FieldValue == "on")
                        return "[X]";
                    else
                        return "[ ]";
                }

                return controlXState.FieldValue;
            }
            set
            {
                controlXState.FieldValue = value;
            }
        }

        public string FieldOptions
        {
            get { return controlXState.FieldOptions; }
            set { controlXState.FieldOptions = value; }
        }

        public FieldType FieldType
        {
            get { return controlXState.FieldType; }
            set { controlXState.FieldType = value; }
        }

        private ControlXState controlXState;

        protected override void Render(HtmlTextWriter output)
        {
            if (controlXState != null)
                AddContol();

            Control.RenderControl(output);
        }


        private void AddContol()
        {
            switch (controlXState.FieldType)
            {
                case FieldType.Label:
                    Literal literal = new Literal();
                    if (!Controls.Contains(literal))
                        Controls.Add(literal);
                    literal.ID = this.ID + "CustomControl";
                    literal.Text = controlXState.FieldValue;
                    Control = literal;
                    break;
                case FieldType.TextBox:
                    TextBox textBox = new TextBox();
                    if (!this.Controls.Contains(textBox))
                        this.Controls.Add(textBox);
                    textBox.ID = this.ID + "CustomControl";
                    textBox.Text = controlXState.FieldValue;
                    Control = textBox;
                    break;
                case FieldType.DropDown:
                    DropDownList dropDownList = new DropDownList();
                    if (!Controls.Contains(dropDownList))
                        Controls.Add(dropDownList);
                    dropDownList.ID = this.ID + "CustomControl";

                    controlXState.FieldOptions.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(s => dropDownList.Items.Add(s));

                    ListItem listItem = dropDownList.Items.FindByText(controlXState.FieldValue);
                    if (listItem != null)
                        listItem.Selected = true;

                    Control = dropDownList;
                    break;
                case FieldType.CheckBox:
                    CheckBox checkBox = new CheckBox();
                    if (!Controls.Contains(checkBox))
                        Controls.Add(checkBox);
                    checkBox.ID = this.ID + "CustomControl";
                    checkBox.Checked = (controlXState.FieldValue == "on" || controlXState.FieldValue == "[X]");
                    Control = checkBox;
                    break;
                case FieldType.NA:
                default:
                    break;
            }
        }

        protected override object SaveControlState()
        {
            object obj = base.SaveControlState();

            if (controlXState != null)
            {
                if (obj != null)
                {
                    return new Pair(obj, controlXState);
                }
                else
                {
                    return (controlXState);
                }
            }
            else
            {
                return obj;
            }
        }

        protected override void LoadControlState(object state)
        {
            if (state != null)
            {
                Pair p = state as Pair;
                if (p != null)
                {
                    base.LoadControlState(p.First);
                    controlXState = (ControlXState)p.Second;
                    if (HttpContext.Current.Request[this.UniqueID + "CustomControl"] != null)
                        controlXState.FieldValue = HttpContext.Current.Request[this.UniqueID + "CustomControl"] + "";
                    else
                        controlXState.FieldValue = string.Empty;
                }
                else
                {
                    if (state is ControlXState)
                    {
                        controlXState = (ControlXState)state;
                        if (HttpContext.Current.Request[this.UniqueID + "CustomControl"] != null)
                            controlXState.FieldValue = HttpContext.Current.Request[this.UniqueID + "CustomControl"] + "";
                        else
                            controlXState.FieldValue = string.Empty;
                    }
                    else
                    {
                        base.LoadControlState(state);
                    }
                }
            }
        }


    }



}
